package com.yihuier.yihuier.Mapper.MeetingManager;

import com.yihuier.yihuier.Entity.MeetingManager.DTO.MeetingDTO;
import com.yihuier.yihuier.Entity.MeetingManager.Meeting;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mapper
public interface MeetingMapper {

    @Results({
            @Result(property = "meeting_id",column = "meeting_id"),
            @Result(property = "name",column = "name"),
            @Result(property = "theme",column = "theme"),
            @Result(property = "description",column = "description"),
            @Result(property = "location",column = "location"),
            @Result(property = "em_number",column = "em_number"),
            @Result(property = "status",column = "status"),
            @Result(property = "applicant",column = "applicant"),
    })
    @Select("SELECT meeting_id,name,theme,description,location,em_number,status,applicant FROM `bz_meeting` ORDER BY meeting_id DESC LIMIT #{offset},#{rows};")
    List<Meeting> selectLatestMeeting(@Param("offset") int offset, @Param("rows") int rows);

    @Select("SELECT m.meeting_id,m.meeting_name,r.meeting_time_start,r.meeting_time,m.location,m.description,m.meeting_status FROM bz_meeting_user u,bz_meeting m,bz_meeting_room r WHERE u.meeting_id = m.meeting_id AND m.meeting_status < 3 AND m.meeting_id = r.meeting_id AND user_id = #{user_id} ORDER BY r.meeting_time_start ASC LIMIT 2;")
    ArrayList<Map<String,Object>> selectLatestTwoMeetingInfomation(@Param("user_id") int user_id);

    @Select("SELECT m.meeting_id,m.meeting_name,r.meeting_time_start,r.meeting_time,m.location,m.description,m.meeting_status FROM bz_meeting_user u,bz_meeting m,bz_meeting_room r WHERE u.meeting_id = m.meeting_id AND m.meeting_status < 3 AND m.meeting_id = r.meeting_id AND user_id = #{user_id} ORDER BY r.meeting_time_start ASC;")
    ArrayList<Map<String,Object>> selectAllMeetingInfomation(@Param("user_id") int user_id);

    @Select("<script>" +
            " SELECT COUNT(meeting_id)" +
            " FROM" +
            " bz_meeting" +
            " WHERE" +
            " meeting_status = 0 AND" +
            " meeting_id IN" +
            " <foreach item='item' index='index' collection='meeting_id' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach> " +
            " </script> ")
    int selectNumOfReadyMeetingByUser(@Param("meeting_id") int[] meeting_id);

    @Select("<script>" +
            " SELECT COUNT(meeting_id)" +
            " FROM" +
            " bz_meeting" +
            " WHERE" +
            " meeting_status = 3 AND" +
            " meeting_id IN" +
            " <foreach item='item' index='index' collection='meeting_id' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach> " +
            " </script> ")
    int selectNumOfClosedMeetingByUser(@Param("meeting_id") int[] meeting_id);

    @Select("<script>" +
            " SELECT COUNT(meeting_id)" +
            " FROM" +
            " bz_meeting" +
            " WHERE" +
            " meeting_status = 4 AND" +
            " meeting_id IN" +
            " <foreach item='item' index='index' collection='meeting_id' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach> " +
            " </script> ")
    int selectNumOfInvalidMeetingByUser(@Param("meeting_id") int[] meeting_id);



    @Results({
            @Result(property = "meeting_id",column = "meeting_id"),
            @Result(property = "name",column = "meeting_name"),
            @Result(property = "description",column = "description"),
            @Result(property = "location",column = "location"),
            @Result(property = "em_number",column = "em_number"),
            @Result(property = "status",column = "meeting_status"),
            @Result(property = "applicant",column = "applicant"),
            @Result(property = "startTime",column = "meeting_time_start"),
            @Result(property = "lastTime",column = "meeting_time"),
            @Result(property = "pic_url",column = "room_pic_url"),
    })
    @Select("<script>" +
            " SELECT a.meeting_id,a.meeting_name,a.description,a.location,a.em_number,a.meeting_status,a.applicant,b.meeting_time_start,b.meeting_time,c.room_pic_url" +
            " FROM" +
            " bz_meeting a ,bz_meeting_room b,bz_room c" +
            " WHERE" +
            " a.meeting_id = b.meeting_id AND" +
            " b.room_id = c.room_id AND" +
            " a.meeting_status = #{status} AND" +
            " a.meeting_id IN" +
            " <foreach item='item' index='index' collection='meeting_id' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach> " +
            " </script> ")
    List<MeetingDTO> selectMeetingByUserAndStatus(@Param("meeting_id") int[] meeting_id,@Param("status")int status);


    /**
     * 根据meeting_id数组获取我收藏的会议一部分信息
     * */
    @Select("<script>" +
            " SELECT meeting_id,meeting_name,description,location" +
            " FROM" +
            " bz_meeting" +
            " WHERE" +
            " meeting_id IN" +
            " <foreach item='item' index='index' collection='meeting_ids' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach> " +
            " </script> ")
    List<Map<String,Object>> selectCollectionMeetingsByMeetingIdArray(@Param("meeting_ids") int[] meeting_ids);

    /**
     * 根据单个meeting_id获取会议一部分信息
     * */
    @Select("SELECT meeting_id,meeting_name,description,location,meeting_status FROM bz_meeting WHERE meeting_id = #{meeting_id};")
    Map<String,Object> selectCollectionMeetingInfoByMeetingId(@Param("meeting_id") Long meeting_id);

    @Select("SELECT meeting_id,meeting_name,description,location,meeting_status FROM bz_meeting WHERE meeting_id = #{meeting_id};")
    Map<String,Object> selectMeetingInfoByMeetingId(@Param("meeting_id") int meeting_id);

    @Insert("INSERT INTO bz_meeting(meeting_name,em_number,meeting_status,applicant,description) " +
            "VALUES(#{meeting_name},#{em_number},#{meeting_status},#{applicant},#{description}); ")
    int addOneBasicMeeting(
            @Param("meeting_name") String meetingName
            ,@Param("em_number") int em_number
            ,@Param("meeting_status") int meeting_status
            ,@Param("applicant") int user_id
            ,@Param("description")String description);

    @Select("SELECT MAX(meeting_id) FROM bz_meeting;")
    int selectOneLastMeetingId();

    /**
     * 得到我参加的会议信息
     * */
    @Select("SELECT m.meeting_id,m.meeting_name,r.meeting_time_start,r.meeting_time,m.location,m.description,m.meeting_status FROM bz_meeting_user u,bz_meeting m,bz_meeting_room r WHERE u.meeting_id = m.meeting_id AND m.meeting_status <= 3 AND m.meeting_id = r.meeting_id AND u.user_id = #{user_id} ORDER BY r.meeting_time_start ASC;")
    ArrayList<Map<String,Object>> selectRelatedToMeMeetingInfomation(@Param("user_id") int user_id);

    /**
    * 根据用户输入内容模糊查询与我有关的会议
    * */
    @Select("SELECT m.meeting_id,m.meeting_name FROM bz_meeting_user u,bz_meeting m,bz_meeting_room r WHERE u.meeting_id = m.meeting_id AND m.meeting_status <= 3 AND m.meeting_id = r.meeting_id AND u.user_id = #{user_id} AND  m.meeting_name like '%${content}%' ORDER BY r.meeting_time_start ASC;")
    ArrayList<Map<String,Object>> selectRelatedToMeMeetingIdAndMeetingNameByVague(@Param("content")String content,@Param("user_id") int user_id);

    @Select("SELECT location FROM bz_meeting WHERE meeting_id = #{meeting_id} ;")
    int selectLocationByMeetingId(@Param("meeting_id") int meeting_id);


    @Update("UPDATE bz_meeting SET location = #{location} WHERE meeting_id = #{meeting_id};")
    int updateMeetingLocation(@Param("meeting_id")int meeting_id,@Param("location")int location);

    @Update("UPDATE bz_meeting SET meeting_status = #{meeting_status} WHERE meeting_id = #{meeting_id};")
    int updateMeetingStatus(@Param("meeting_id")int meeting_id,@Param("meeting_status")int meeting_status);

    @Update("UPDATE bz_meeting SET em_number = em_number + 1 WHERE meeting_id = #{meeting_id};")
    int addOneUserIntoMeeting(@Param("meeting_id")int meeting_id);

    @Select("SELECT meeting_name FROM bz_meeting WHERE meeting_id = #{meeting_id};")
    String selectMeetingNameById(@Param("meeting_id")int meeting_id);
}

